package com.example.demo.controller;

import com.example.demo.Dao.DaoFactory;
import com.example.demo.bean.Goods;
import com.example.demo.bean.Goods_temp;
import com.example.demo.bean.Records;
import com.example.demo.service.GoodsDao;
import com.example.demo.service.GoodsDao_temp;
import com.example.demo.service.RecordsDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import sun.util.resources.ga.LocaleNames_ga;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@RestController
@CrossOrigin
@RequestMapping("/records")
public class RecordsController {
    @Autowired
    GoodsDao goodsdao = DaoFactory.getGoodsDao();

    @Autowired
    RecordsDao dao= DaoFactory.getRecordsDao();

    @Autowired
    GoodsDao_temp dao_temp = DaoFactory.getGoodsDao_temp();

    @RequestMapping("/index")
    public List<Records> index(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception{
        List<Records> list=dao.findGoodsList();
        return  list;
    }

    @RequestMapping("/add")
    public boolean add(HttpServletRequest request, HttpServletResponse response , Model m) throws Exception{
        String[] info=request.getParameterValues("info");   //获取url后的info字符串数组
        double random = Math.round(Math.random()*1000000);
        Long orderid = new Double(random).longValue();        //随机生成订单编号
        for(String str:info){                              //遍历info数组,若是face则为人脸,若为bread则是面包信息
            String[] temp=str.split(",");
//            System.out.println(Arrays.toString(temp));
            if(temp[0].equals("face")){
                Records records=new Records();
                Long id=Long.parseLong(temp[1].substring(temp[1].indexOf(":")+1));
                double score=Double.parseDouble(temp[2].substring(temp[2].indexOf(":")+1));
                String idclass=temp[3].substring(temp[3].indexOf(":")+1);
                int age=Integer.parseInt(temp[4].substring(temp[4].indexOf(":")+1));
                String gender=temp[5].substring(temp[5].indexOf(":")+1);
                records.setOrderid(orderid);
                records.setId(id);
                records.setScore(score);
                records.setIdclass(idclass);
                records.setAge(age);
                records.setGender(gender);
                dao.add(records);
            }
            if(temp[0].equals("bread")){
                Goods_temp goods_temp=new Goods_temp();
                Long id=Long.parseLong(temp[1].substring(temp[1].indexOf(":")+1));
                double score=Double.parseDouble(temp[2].substring(temp[2].indexOf(":")+1));
                String idclass=temp[3].substring(temp[3].indexOf(":")+1);
                if(dao_temp.checkId(id,orderid)){                                        //判断订单中是否已经有该面包信息,如果有只需数量+1
                    List<Goods_temp> list_temp= dao_temp.findGoodsById(id,orderid);
                    Goods_temp goods_temp2=list_temp.get(0);
                    int n=goods_temp2.getAmount();
                    n=n+1;
                    goods_temp2.setAmount(n);
                    dao_temp.update(goods_temp2);
                }
                else{                                                          //若没有,则从bread数据库中拉取面包id对应的name,price信息
                    List<Goods> list= goodsdao.findGoodsById(id);
                    if(list==null || list.size()==0){
                        return false;
                    }
                    Goods goods=list.get(0);
//                    goods_temp.setName(goods.getName());
//                    goods_temp.setPrice(goods.getPrice());
                    switch (idclass) {
                        case "c1":
                            goods_temp.setPrice(10);
                            goods_temp.setName("圆面包");
                            break;
                        case "c2":
                            goods_temp.setPrice(20);
                            goods_temp.setName("棒面包");
                            break;
                        case "c3":
                            goods_temp.setPrice(30);
                            goods_temp.setName("圈面包");
                            break;
                    }
                    goods_temp.setId(id);                                     //将剩下的id,score,idclass,orderid全部填到对象good_temp中
                    goods_temp.setScore(score);
                    goods_temp.setIdclass(idclass);
                    goods_temp.setOrderid(orderid);
                    dao_temp.add(goods_temp);
                }
            }
        }
//        Long id=Long.parseLong(temp[1].substring(temp[1].indexOf(":")+1));
//        System.out.println(id);
        return true;
    }
    @RequestMapping("/delete")
    public boolean delete(HttpServletRequest request,HttpServletResponse response,Model m)throws Exception{
        long id_delete = Integer.parseInt(request.getParameter("orderid"));
        dao.del(id_delete);
        dao_temp.del(id_delete);
        return true;
    }
}
